package cn.elricyu.kf.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.elricyu.kf.bean.SnType;
import cn.elricyu.kf.dao.SnTypeDAO;
import cn.elricyu.kf.exception.ServiceException;

public class SnTypeService {

	private Map<String, SnType> sntypeMap = new HashMap<String, SnType>();
	private SnTypeDAO snTypeDao;

	public SnType saveSnType(SnType sntype) throws ServiceException {
		try {
			snTypeDao.saveSnType(sntype);
		} catch (Exception e) {
			throw new ServiceException("SunsntypeService.saveSnType", e);
		}
		return sntype;
	}

	public SnType updateSnType(SnType sntype) throws ServiceException {
		try {
			snTypeDao.updateSnType(sntype);
		} catch (Exception e) {
			throw new ServiceException("SunsntypeService.updateSnType", e);
		}
		return sntype;
	}

	public void deleteSnType(Integer id) throws ServiceException {
		try {
			snTypeDao.deleteSnType(id);
		} catch (Exception e) {
			throw new ServiceException("SunsntypeService.deleteSnType", e);
		}
	}

	public List<SnType> getAllSnTypes() throws ServiceException {
		List<SnType> sntypes;
		try {
			sntypes = snTypeDao.getAllSnTypes();
		} catch (Exception e) {
			throw new ServiceException("SunsntypeService.getAllSnTypes", e);
		}
		return sntypes;
	}

	public SnType getSnType(Integer id) throws ServiceException {
		SnType sntype;
		try {
			sntype = snTypeDao.getSnType(id);
		} catch (Exception e) {
			throw new ServiceException("SunsntypeService.getSnType", e);
		}
		return sntype;
	}

	public SnType getSnTypeByAppName(String appName) throws ServiceException {
		SnType sntype;
		try {
			sntype = snTypeDao.getSnTypeByAppName(appName);
		} catch (Exception e) {
			throw new ServiceException("SunsntypeService.getSnTypeByAppName", e);
		}
		return sntype;
	}

	public SnType getSnTypeByType(String type) throws ServiceException {
		SnType sntype = sntypeMap.get(type);
		if (sntype == null) {
			try {
				sntype = snTypeDao.getSnTypeByType(type);
				if (sntype != null) {
					sntypeMap.put(type, sntype);
				}
			} catch (Exception e) {
				throw new ServiceException("SunsntypeService.getSnTypeByType",
						e);
			}
		}
		return sntype;
	}

	public SnTypeDAO getSnTypeDao() {
		return snTypeDao;
	}

	public void setSnTypeDao(SnTypeDAO snTypeDao) {
		this.snTypeDao = snTypeDao;
	}

}
